const app=getApp();
const imgBaseUrl=app.globalData.imgBaseUrl;
const imgPreUrl=app.globalData.imgPreUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgBaseUrl: imgBaseUrl,
    imgPreUrl: imgPreUrl,
    vip:['您还不是会员','月会员','季会员','年会员'],
    recharge: []
  },
  onLoad: function (){

    this.getrechargeDetail();
  },
  onShow: function (options) {
    var that = this;
    var isLogin = app.isLogin();
    this.setData({
      isLogin: isLogin
    })
    if (isLogin) {
      this.getUser();
    }
  },
  getUser:function(){
    var that=this;
    app.func.postReq('/text/index.php/home/user/userinfo', {}, function (res) {
      console.log(res);
      that.setData({
        grade: res.user.grade,
        name: res.user.nickname,
        avatar: res.user.avatar,
        money:res.user.money,
        dfk:res.dfk_num,
        dfh:res.dsh_num,
        dqr:res.dqr_num,
        ywc:res.ywc_num
      })
    })
  },
  getUserInfo: function (e) {
    var that = this;
    var data = {};
    if (e.detail.errMsg == "getUserInfo:ok") {
      that.login(e.detail.userInfo, e);
    }
  },
  oginooogin: function (data, e) {
    var that = this;
    wx.login({
      success: res => {
        data.code = res.code;
        console.log(data);
        app.func.postReq('/text/index.php/home/login/adduser', data, function (res) {
          if (res) {
            wx.setStorageSync('hhh', res.users.id)
            that.setData({ 
              isLogin: 1
            })
            that.getUser();
            app.func.postReq('/text/index.php/home/shopcar/cars', {}, function (res) {
              if (parseFloat(res) > 0) {
                wx.setTabBarBadge({
                  index: 2,
                  text: res.toString(),
                })
              }
            })
          }
        })
      }
    })
  },
  bindNavTo:function(e){
    var url=e.currentTarget.dataset.url;
    if(this.data.isLogin){
      wx.navigateTo({
        url: url
      })
    }else{
      wx.showToast({
        title: '您还没有登录！',
        icon:'none'
      })
    }
  },
  getrechargeDetail: function (e){

    var that = this;
    app.func.getReq('/text/index.php/home/Recharge/listinfo', {}, function(res) {

        if(res.content){

          that.setData({

            recharge: JSON.parse(res.content)
          });
        }

    });
  }
});